package com.shyroke.service.serviceImpl;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shyroke.common.DateUtils;
import com.shyroke.common.JsonConvert;
import com.shyroke.entity.Article;
import com.shyroke.entity.User;
import com.shyroke.mapper.ArticleMapper;
import com.shyroke.mapper.LabelMapper;
import com.shyroke.mapper.UserMapper;
import com.shyroke.service.ArticleServiceI;

@Service
public class ArticleServiceImpl implements ArticleServiceI {

	@Autowired
	private ArticleMapper articleMapper;
	
	@Autowired
	private LabelMapper labelMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public List<Article> selectShowArticleByLabelId(Integer labelId) {
		return articleMapper.selectShowArticleByLabelId(labelId);
	}

	@Override
	public List<Article> selectAll(Integer userid) {
		return articleMapper.selectAll(userid);
	}

	@Override
	public void insert(Article article,HttpSession session) throws IOException {
		User user=(User)session.getAttribute("user");
		
		JsonConvert.changeUrl("/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}");
		article.setUserid(user.getId());
		article.setAuthor(user.getUsername());
		article.setCreateTime(DateUtils.getFormatCurrenDate());
		article.setIsshenhe("否");
		article.setIsshow("否");
		
		
		//根据label查找labelId
		int labelId=labelMapper.selectLabelIdByLabel(article.getLabel());
		article.setLabelId(labelId);
		
		article.setPinglunMount(0);
		article.setShoucangMount(0);
		article.setYueduMount(0);
		article.setZhuanzaiMount(0);
		
		articleMapper.insert(article);
	}

	@Override
	public Article selectById(Integer articleId) {
		return articleMapper.selectByPrimaryKey(articleId);
	}

	@Override
	public void delById(Integer articleId) {
		articleMapper.deleteByPrimaryKey(articleId);
		
	}

	@Override
	public void shoucang(Article article, HttpSession session) {
		
		User user=(User)session.getAttribute("user");
		Article oldArticle=articleMapper.selectByPrimaryKey(article.getId());
		article.setUserid(user.getId());
		article.setIsshenhe("是");
		article.setIsshow("否");
		article.setLabelId(labelMapper.selectLabelIdByLabel(article.getLabel()));
		article.setCreateTime(DateUtils.getFormatCurrenDate());
		article.setPinglunMount(oldArticle.getPinglunMount());
		article.setShoucangMount(oldArticle.getShoucangMount());
		article.setYueduMount(oldArticle.getYueduMount());
		article.setZhuanzaiMount(oldArticle.getZhuanzaiMount());
		article.setAuthor(oldArticle.getAuthor());
		article.setContent(oldArticle.getContent());
		article.setId(null);
		articleMapper.insert(article);
	}

	@Override
	public boolean isYuanChuang(Integer articleId) {
		Article article=articleMapper.selectByPrimaryKey(articleId);
		
		int userid=article.getUserid();
		String userName=article.getAuthor();
		
		User user=userMapper.isExitByUserIdAndUserName(userid,userName);
		
		if(user!=null) {
			//说明该文章为原创
			return true;
		}else {
			//非原创
			return false;
		}
		
	
	}

	@Override
	public void uptArticle(Article article) {
		if(article.getWenjianjia().equals("")) {
			article.setWenjianjia(null);
		}
		articleMapper.updateByPrimaryarticleKeyelective(article);
	}

	@Override
	public List<String> selectWenjianjiaByUserId(Integer id) {
		
		return articleMapper.selectWenjianjiaByUserId(id);
	}

	@Override
	public List<Article> selctAllByWenjianjia(Integer id, String wenjianjia) {
		return articleMapper.selctAllByWenjianjia(id,wenjianjia);
	}

	@Override
	public void addMount(String string, Integer articleId) {

		Article article=articleMapper.selectByPrimaryKey(articleId);
		int mount=0;
		if(string.contains("评论")) {
			//评论数加一
			mount=article.getPinglunMount();
			mount=mount+1;
			article.setPinglunMount(mount);
		}else if(string.contains("阅读")) {
			//阅读数加一
			mount=article.getYueduMount();
			mount=mount+1;
			article.setYueduMount(mount);
		}else if(string.contains("收藏")) {
			//收藏数加一
			mount=article.getShoucangMount();
			mount=mount+1;
			article.setShoucangMount(mount);
		}
		
		articleMapper.updateByPrimaryarticleKeyelective(article);
		
	}

	@Override
	public List<Article> selctAllByPublicWenjianjia(Integer id, String wenjianjia) {
		return articleMapper.selctAllByPublicWenjianjia(id,wenjianjia);
	}

	@Override
	public List<Article> selectAllPublic(Integer id) {
		return articleMapper.selectAllPublic(id);
	}

	@Override
	public List<Article> selectAllNoShenHe() {
		return articleMapper.selectAllNoShenHe();
	}

	@Override
	public List<Article> selectNoShenHeByAuthor(String author) {
		return articleMapper.selectNoShenHeByAuthor(author);
	}

	@Override
	public List<Article> selectBySearchTitleAndContent(String key) {
		return articleMapper.selectBySearchTitleAndContent(key);
	}

	@Override
	public List<Article> selectMyLibBySearchTitleAndContent(String key, Integer id) {
		return articleMapper.selectMyLibBySearchTitleAndContent(key,id);
	}

	@Override
	public void shoucangByother(Article article, HttpSession session) {
		User user=(User)session.getAttribute("user");
		article.setUserid(user.getId());
		article.setIsshenhe("是");
		article.setIsshow("否");
		article.setLabelId(labelMapper.selectLabelIdByLabel(article.getLabel()));
		article.setCreateTime(DateUtils.getFormatCurrenDate());
		article.setAuthor("站外");
		article.setId(null);
		articleMapper.insert(article);
	}

	@Override
	public List<String> selectWenjianjiaByUserIdPublic(Integer userId) {
		
		return articleMapper.selectWenjianjiaByUserIdPublic(userId);
	}



}
